package com.sakiko.memory.controller;

import cn.hutool.core.date.DateUtil;
import com.sakiko.memory.service.ChatAssistant;
import com.sakiko.memory.service.ChatMemoryAssistant;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * projectName: com.sakiko.memory.controller
 *
 * @author: sakiko
 * time: 2025/8/28 1:56
 * description: 记忆缓存
 */
@RestController
@Slf4j
public class ChatMemoryController {
    @Resource(name = "chat")
    ChatAssistant chatAssistant;

    @Resource(name = "chat-withMemory-Window")
    ChatMemoryAssistant chatMemoryAssistantWindow;

    @Resource(name = "chat-withMemory-Token")
    ChatMemoryAssistant chatMemoryAssistantToken;

    // v1: 没有记忆缓存
    // http://localhost:9008/chatmemory/test1
    @GetMapping("chatmemory/test1")
    public String chat1() {
        String answer1 = chatAssistant.chat("你好，我的名字叫 sakiko,pls remember");
        System.out.println("answer1 = " + answer1);

        String answer2 = chatAssistant.chat("你记住了我之前说的名字吗？");
        System.out.println("answer2 = " + answer2);

        return "success : " + DateUtil.now() + "<br> \n\n answer01: " + answer1 + "<br> \n\n answer02: " + answer2;
    }


    // v2: 有记忆缓存 - MessageWindow
    // http://localhost:9008/chatmemory/test2
    @GetMapping("chatmemory/test2")
    public String chatMessageWindowChatMemory() {
        chatMemoryAssistantWindow.chatWithChatMemory(1L, "你好！我的名字是Java.");
        String answer01 = chatMemoryAssistantWindow.chatWithChatMemory(1L, "我的名字是什么");
        System.out.println("answer01返回结果：" + answer01);

        chatMemoryAssistantWindow.chatWithChatMemory(3L, "你好！我的名字是C++");
        String answer02 = chatMemoryAssistantWindow.chatWithChatMemory(3L, "我的名字是什么");
        System.out.println("answer02返回结果：" + answer02);

        return "chatMessageWindowChatMemory success : "
                + DateUtil.now() + "<br> \n\n answer01: " + answer01 + "<br> \n\n answer02: " + answer02;

    }


    // v3: 有记忆缓存 - MessageToken
    // http://localhost:9008/chatmemory/test3
    @GetMapping("chatmemory/test3")
    public String chatTokenWindowChatMemory() {
        chatMemoryAssistantToken.chatWithChatMemory(1L, "你好！我的名字是mysql");
        String answer01 = chatMemoryAssistantToken.chatWithChatMemory(1L, "我的名字是什么");
        System.out.println("answer01返回结果：" + answer01);

        chatMemoryAssistantToken.chatWithChatMemory(3L, "你好！我的名字是oracle");
        String answer02 = chatMemoryAssistantToken.chatWithChatMemory(3L, "我的名字是什么");
        System.out.println("answer02返回结果：" + answer02);

        return "chatTokenWindowChatMemory success : "
                + DateUtil.now() + "<br> \n\n answer01: " + answer01 + "<br> \n\n answer02: " + answer02;
    }
}
